﻿using System;
using Smart.Model;
using UnityEngine;

namespace Smart.Hotfix
{
    public static partial class HotfixEntry
    {
        public static void Awake()
        {
            Entry.Instance = new Entry(new GameObject(string.Empty));

            var loaders = Entry.Instance.EventSystem.GetTableLoaders();
            for(int i = 0,max = loaders.Count;i<max;++i)
            {
                Model.Entry.Instance.TableComponent.Register(loaders[i].FileName, loaders[i].Init, loaders[i].CreateAndBind, loaders[i].Bind);
            }
        }

        public static void EATest(int msgid,object argv)
        {
            if(argv is string words)
            {
                Logger.Log(words);
            }
        }

        public static void Start()
        {
            try
            {
                Entry.Instance.TableComponent.LoadAllTables();
                HotfixManager.Instance.Ready = true;
                long guid = Entry.Instance.FrameComponent.OpenFrame<UILoginFrame>("UIMainPanel".ToLower(), "UIMainPanel".ToLower(), 9527);
                Entry.Instance.FrameComponent.CloseFrame(guid);
            }
            catch (Exception e)
            {
                UnityEngine.Debug.LogError(e.Message);
            }
        }

        public static void Update()
        {
            Entry.Instance.EventSystem.Update();
        }

        public static void LateUpdate()
        {
            Entry.Instance.EventSystem.LateUpdate();
        }

        public static void Destroy()
        {
            Entry.Instance.Dispose();
            Entry.Instance = null;
        }
    }
}
